package com.reshare.service;


import com.github.pagehelper.util.StringUtil;
import com.reshare.base.BaseService;
import com.reshare.bean.Category;
import com.reshare.mapper.CategoryMapper;
import com.reshare.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsCategoryService extends BaseService<Category,Integer> {

    @Resource
    private CategoryMapper categoryMapper;

     public  void addCategory(Category category) {
         AssertUtil.isTrue(StringUtils.isBlank(category.getCateName()), "请输入分类名称");
         Category temp = categoryMapper.selectCategoryByName(category.getCateName());
         AssertUtil.isTrue(temp!=null,"分类已经存在");
         category.setCreateTime(new Date());
         category.setUpdateTime(new Date());
         AssertUtil.isTrue(categoryMapper.insertSelective(category)<1,"分类添加失败");


     }
     public void changeCategory(Category category){
         AssertUtil.isTrue(categoryMapper.selectByPrimaryKey(category.getId())==null||category.getId()==null,"分类名不存在");
         Category temp=categoryMapper.selectCategoryByName(category.getCateName());
         AssertUtil.isTrue(temp!=null&&!(temp.getId().equals(category.getId())),"分类已经存在");
         category.setUpdateTime(new Date());
         AssertUtil.isTrue(categoryMapper.updateByPrimaryKey(category)<1,"分类修改失败");

     }

     public Map<String,Object> findAllCategory(){
         List<Category> clist=categoryMapper.selectAllCategory();

         Map<String,Object> map=new HashMap<>();
         map.put("code", 0);
         map.put("msg", "success");
         map.put("count", clist.size());
         map.put("data", clist);
         //返回目标数据
         return map;

     }

     public void removCategoryById(Integer id){
         AssertUtil.isTrue(id==null||categoryMapper.selectByPrimaryKey(id)==null,"请求选择删除数据");

         AssertUtil.isTrue(categoryMapper.deleteByPrimaryKey(id)!=1,"删除失败");


     }


}
